/*
*   @Author Mangodai
*   @Date 8/30/2017 9:32 PM
* */
$(function () {
    urlinfo = window.location.search;
    if (urlinfo == null || urlinfo == undefined || urlinfo == '' || urlinfo.length < 2) {
        alert("参数错误！");
        return;
    }
    var data = getQueryObject();
    $('#fileName').html(data.name);
    var url = "memberInfo/" + data.th + "/" + data.name;
    $.get(url, function (data) {
        if (data == null || data == undefined || data == '' || data.length < 2) {
            alert('地址错误');
            window.location.href = "index.html";
            return;
        }
        // var toc = [];
        var renderer = (function () {
            var renderer = new marked.Renderer();
            renderer.heading = function (text, level, raw) {
                var anchor = this.options.headerPrefix + raw.toLowerCase().replace(/[^\w]+/g, '-');
                // toc.push({
                //     anchor: anchor,
                //     level: level,
                //     text: text
                // });
                return '<h'
                    + level
                    + ' id="'
                    + anchor
                    + '">'
                    + text
                    + '</h'
                    + level
                    + '>\n';
            };
            return renderer;
        })();
        marked.setOptions({
            renderer: renderer,
            gfm: true,
            tables: true,
            breaks: true,
            pedantic: false,
            sanitize: true,
            smartLists: true,
            smartypants: false
        });
        marked.setOptions({
            highlight: function (code) {
                return hljs.highlightAuto(code).value;
            }
        });
        var tocFlag = false;
        if (-1 != data.indexOf('[TOC]')) {
            tocFlag = true;
            data = data.replace('[TOC]', '')
        }
        var parse = marked(data);
        // var tocHTML = '<h1 id="table-of-contents">TOC</h1>\n<ul>';
        // var tocHTML = '<h1 id="table-of-contents">TOC</h1>\n';
        // toc.forEach(function (entry) {
        //     // tocHTML += '<li><a href="#'+toc[0].anchor+'">'+toc[0].text+'<a></li>\n';
        //     tocHTML += '<li><a href="#' + entry.anchor + '">' + entry.text + '<a></li>\n';
        // });
        // for (var i = 0; i < toc.length; i++) {
        // tocHTML += getHTML(toc, 0);
        // }
        // tocHTML += '</ul>\n';

        $('.markdown-body').append(parse);
        if (tocFlag) {
            // $('.markdown-body').append(tocHTML);
            $('#toc').toc({ listType: 'ul' });
        }   
    });
});

function getparams(data) {
    var param = data.split("&");
    if (param.size == 2) {
        return param;
    } else {
        return null;
    }
}

function getQueryObject() {
    var search = window.location.search;
    var obj = {};
    var reg = /([^?&=]+)=([^?&=]*)/g;
    search.replace(reg, function (rs, $1, $2) {
        var name = decodeURIComponent($1);
        var val = decodeURIComponent($2);
        val = String(val);
        obj[name] = val;
        return rs;
    });
    return obj;
}